package com.zx.mes.hyl.common.log.aspectj;

import com.zx.mes.hyl.common.log.annotation.AroundLog;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * around 环绕日志打印
 * @author hyl
 * @date 2018-5-9
 */
@Aspect
@Component
public class AroundLogAspectJ {

    private Logger logger = null;

    @Around("execution(* com.zx.mes..*.*(..)) && @annotation(aroundLog)")
    public Object around(ProceedingJoinPoint pjd, AroundLog aroundLog){
        long timeMillis = System.currentTimeMillis();
        logger = LoggerFactory.getLogger(aroundLog.type());
        String methodName = pjd.getSignature().getName();
        logger.info("方法名:[{}],开始执行",methodName);
        Object result=null;
        try {
            result = pjd.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        logger.info("方法名:[{}],执行完成",methodName);
        long timeMillis2 = System.currentTimeMillis();
        logger.info("方法名:[{}],执行时间[{}]ms",methodName,timeMillis2-timeMillis);
        return result;
    }
}
